
'"AT" means "@"
'指令集'
' BSET 返回将某位置1后的值
'BCLR 返回将某位清零后的值'
#define BSET(val,n) ( (val) or (cast(typeof(val),1) shl (n)))
#define BCLR(val,n) ( (val) and not (cast(typeof(val),1) shl (n)))
'Instruction 指令代码'
enum Instruction
    ADD_A_R0 = &H28
    ADD_A_R1 = &H29
    ADD_A_R2 = &H2A
    ADD_A_R3 = &H2B
    ADD_A_R4 = &H2C
    ADD_A_R5 = &H2D
    ADD_A_R6 = &H2E
    ADD_A_R7 = &H2F
    ADD_A_DIRECT = &H25
    ADD_A_ATR0 = &H26
    ADD_A_ATR1 = &H27
    ADD_A_DATA =&H24
    ADDC_A_R0 = &H38
    ADDC_A_R1 = &H39
    ADDC_A_R2 = &H3A
    ADDC_A_R3 = &H3B
    ADDC_A_R4 = &H3C
    ADDC_A_R5 = &H3D
    ADDC_A_R6 = &H3E
    ADDC_A_R7 = &H3F
    ADDC_A_DIRECT = &H35
    ADDC_A_ATR0 = &H36
    ADDC_A_ATR1 = &H37
    ADDC_A_DATA = &H34
    SUBB_A_R0 = &H98
    SUBB_A_R1 = &H99
    SUBB_A_R2 = &H9A
    SUBB_A_R3 = &H9B
    SUBB_A_R4 = &H9C
    SUBB_A_R5 = &H9D
    SUBB_A_R6 = &H9E
    SUBB_A_R7 = &H9F
    SUBB_A_DIRECT = &H95
    SUBB_A_ATR0 =&H96
    SUBB_A_ATR1 =&H97
    SUBB_A_DATA = &H94
    INC_A =&H04
    INC_R0 = &H08
    INC_R1 =&H09
    INC_R2 = &H0A
    INC_R3 = &H0B
    INC_R4 = &H0C
    INC_R5 = &H0D
    INC_R6 = &H0E
    INC_R7 = &H0F
    INC_DIRECT = &H05
    INC_ATR0 = &H06
    INC_ATR1 =&H07
    INC_DPTR =&H3A
    DEC_A = &H14
    DEC_R0 =&H18
    DEC_R1 =&H19
    DEC_R2 = &H1A
    DEC_R3 = &H1B
    DEC_R4 = &H1C
    DEC_R5 = &H1D
    DEC_R6 = &H1E
    DEC_R7 = &H1F
    DEC_DIRECT = &H15
    DEC_ATR0 = &H16
    DEC_ATR1 = &H17
    MUL_AB = &HA4
    DIV_AB = &H84
    DA_A = &HD4
    ANL_A_R0 = &H58
    ANL_A_R1 = &H59
    ANL_A_R2 = &H5A
    ANL_A_R3 = &H5B
    ANL_A_R4 = &H5C
    ANL_A_R5 = &H5D
    ANL_A_R6 = &H5E
    ANL_A_R7 = &H5F
    ANL_A_DIRECT = &H55
    ANL_A_ATR0 = &H56
    ANL_A_ATR1 = &H57
    ANL_A_DATA = &H54
    ANL_DIRECT_A = &H52
    ANL_DIRECT_DATA =&H53
    ORL_A_R0 = &H48
    ORL_A_R1 = &H49
    ORL_A_R2 = &H4A
    ORL_A_R3 = &H4B
    ORL_A_R4 = &H4C
    ORL_A_R5 = &H4D
    ORL_A_R6 = &H4E
    ORL_A_R7 = &H4F
    ORL_A_DIRECT = &H45
    ORL_A_ATR0 =&H46
    ORL_A_ATR1 =&H47
    ORL_A_DATA =&H44
    ORL_DIRECT_A =&H42
    ORL_DIRECT_DATA = &H43
    XRL_A_R0 = &H68
    XRL_A_R1 =&H69
    XRL_A_R2 = &H6A
    XRL_A_R3 = &H6B
    XRL_A_R4 = &H6C
    XRL_A_R5 = &H6D
    XRL_A_R6 = &H6E
    XRL_A_R7 = &H6F
    XRL_A_DIRECT =&H65
    XRL_A_ATR0 =&H66
    XRL_A_ATR1 =&H67
    XRL_A_DATA =&H64
    XRL_DIRECT_A =&H62
    XRL_DIRECT_DATA =&H63
    CLR_A = &HE4
    CPL_A = &HF4
    RL_A = &H23
    RLC_A = &H33
    RR_A =&H03
    RRC_A =&H13
    SWAP_A =&HC4
    MOV_A_R0 = &HE8
    MOV_A_R1 = &HE9
    MOV_A_R2 = &HEA
    MOV_A_R3 = &HEB
    MOV_A_R4 = &HEC
    MOV_A_R5 = &HED
    MOV_A_R6 = &HEE
    MOV_A_R7 = &HEF
    MOV_A_DIRECT =&HE5
    MOV_A_ATR0 = &HE6
    MOV_A_ATR1 = &HE7
    MOV_A_DATA = &H74
    MOV_R0_A = &HF8
    MOV_R1_A = &HF9
    MOV_R2_A = &HFA
    MOV_R3_A = &HFB
    MOV_R4_A = &HFC
    MOV_R5_A = &HFD
    MOV_R6_A = &HFE
    MOV_R7_A = &HFF
    MOV_R0_DIRECT = &HA8
    MOV_R1_DIRECT = &HA9
    MOV_R2_DIRECT = &HAA
    MOV_R3_DIRECT = &HAB
    MOV_R4_DIRECT = &HAC
    MOV_R5_DIRECT = &HAD
    MOV_R6_DIRECT = &HAE
    MOV_R7_DIRECT = &HAF
    MOV_R0_DATA = &H78
    MOV_R1_DATA = &H79
    MOV_R2_DATA = &H7A
    MOV_R3_DATA = &H7B
    MOV_R4_DATA = &H7C
    MOV_R5_DATA = &H7D
    MOV_R6_DATA = &H7E
    MOV_R7_DATA = &H7F
    MOV_DIRECT_A = &HF5
    MOV_DIRECT_R0 = &H88
    MOV_DIRECT_R1 = &H89
    MOV_DIRECT_R2 = &H8A
    MOV_DIRECT_R3 = &H8B
    MOV_DIRECT_R4 = &H8C
    MOV_DIRECT_R5 = &H8D
    MOV_DIRECT_R6 = &H8E
    MOV_DIRECT_R7 = &H8F
    MOV_DIRECT1_DIRECT2 = &H85
    MOV_DIRECT_ATR0 = &H86
    MOV_DIRECT_ATR1 = &H87
    MOV_DIRECT_DATA = &H75
    MOV_ATR0_A = &HF6
    MOV_ATR1_A = &HF7
    MOV_ATR0_DIRECT = &HA6
    MOV_ATR1_DIRECT = &HA7
    MOV_ATR0_DATA =  &H76
    MOV_ATR1_DATA = &H77
    MOV_DPTR_DATA16 = &H90
    MOVC_A_ATADPTR = &H93
    MOVC_A_ATAPC = &H83
    MOVX_A_ATR0 = &HE2
    MOVX_A_ATR1 = &HE3
    MOVX_A_ATDPTR = &HE0
    MOVX_ATR0_A = &HF2
    MOVX_ATR1_A = &HF3
    MOVX_DPTR_A = &HF0
    PUSH_DIRECT = &HC0
    POP_DIRECT = &HD0
    XCH_A_R0 = &HC8
    XCH_A_R1 = &HC9
    XCH_A_R2 = &HCA
    XCH_A_R3 = &HCB
    XCH_A_R4 = &HCC
    XCH_A_R5 = &HCD
    XCH_A_R6 = &HCE
    XCH_A_R7 = &HCF
    XCH_A_DIRECT = &HC5
    XCH_A_ATR0 = &HC6
    XCH_A_ATR1 = &HC7
    XCHD_A_ATR0 = &HD6
    XCHD_A_ATR1 = &HD7
    CLR_C = &HC3
    CLR_BIT = &HC2
    SETB_C = &HD3
    SETB_BIT = &HD2
    CPL_C = &HB3
    CPL_BIT = &HB2
    ANL_C_BIT = &H82
    ANL_C_BIT2 = &HB0
    ORL_C_BIT = &H72
    ORL_C_BIT2 = &HA0
    MOV_C_BIT = &HA2
    MOV_BIT_C = &H92
    LCALL_ADDR16 = &H12
    RET = &H22
    RETI = &H32
    LJMP_ADDR16 = &H02
    SJMP_REL = &H80
    JMP_ATADPTR = &H73
    JZ_REL = &H60
    NJZ_REL = &H70
    JC_REL = &H40
    NJC_REL = &H50
    JB_BIT_REL = &H20
    JNB_BIT_REL = &H30
    JBC_BIT_REL = &H10
    CJNE_A_DIRECT_REL = &HB5
    CJNE_A_DATA_REL = &HB4
    CJNE_R0_DATA_REL = &HB8
    CJNE_R1_DATA_REL = &HB9
    CJNE_R2_DATA_REL = &HBA
    CJNE_R3_DATA_REL = &HBB
    CJNE_R4_DATA_REL = &HBC
    CJNE_R5_DATA_REL = &HBD
    CJNE_R6_DATA_REL = &HBE
    CJNE_R7_DATA_REL = &HBF
    CJNE_ATR0_DATA_REL = &HB6
    CJNE_ATR1_DATA_REL = &HB7
    DJNZ_R0_REL = &HD8
    DJNZ_R1_REL = &HD9
    DJNZ_R2_REL = &HDA
    DJNZ_R3_REL = &HDB
    DJNZ_R4_REL = &HDC
    DJNZ_R5_REL = &HDD
    DJNZ_R6_REL = &HDE
    DJNZ_R7_REL = &HDF
    DJNZ_DIRECT_REL = &HD5
    NOP = &H00
    ACALL_ADDR11_1 = &H11
    ACALL_ADDR11_3 = &H31
    ACALL_ADDR11_5 = &H51
    ACALL_ADDR11_7 = &H71
    ACALL_ADDR11_9 = &H91
    ACALL_ADDR11_B = &HB1
    ACALL_ADDR11_D = &HD1
    ACALL_ADDR11_F = &HF1
    AJMP_ADDR11_0 = &H01
    AJMP_ADDR11_2 = &H21
    AJMP_ADDR11_4 = &H41
    AJMP_ADDR11_6 = &H61
    AJMP_ADDR11_8 = &H81
    AJMP_ADDR11_A = &HA1
    AJMP_ADDR11_C = &HC1
    AJMP_ADDR11_E = &HE1
end enum
const RAM_SIZE  =256 '数据存储器大小'
const ROM_SIZE  =64000 '程序存储器大小'
/'寄存器0组'/
const g0R0 =&H00
const g0R1 =&H01
const g0R2 =&H02
const g0R3 =&H03
const g0R4 =&H04
const g0R5 =&H05
const g0R6 =&H06
const g0R7 =&H07

/'寄存器1组'/
const g1R0 =&H08
const g1R1 =&H09
const g1R2 =&H0A
const g1R3 =&H0B
const g1R4 =&H0C
const g1R5 =&H0D
const g1R6 =&H0E
const g1R7 =&H0F

/'寄存器2组'/
const g2R0 =&H10
const g2R1 =&H11
const g2R2 =&H12
const g2R3 =&H13
const g2R4 =&H14
const g2R5 =&H15
const g2R6 =&H16
const g2R7 =&H17

/'寄存器3组'/
const g3R0 =&H18
const g3R1 =&H19
const g3R2 =&H1A
const g3R3 =&H1B
const g3R4 =&H1C
const g3R5 =&H1D
const g3R6 =&H1E
const g3R7 =&H1F
const regP0 = &H80 '*P0端口'
const regSP = &H81 '堆栈指针'
const regDPL =&H82 '数据指针
const regDPH =&H83 '
const regPCON = &H87 '电源控制'
const regTCON = &H88 '*定时器控制'
const regTMOD = &H89 '定时器模式'
const regTL0 = &H8A 'T0'低字节
const regTL1 =&H8B 'T1低字节'
const regTH0 =&H8C 'T0高字节'
const regTH1 =&H8C 'T1高字节'
const regAUXR =&H8E '辅助寄存器'
const regP1 =&H90 '*P1端口'
const regSCON =&H98 '*串行口控制'
const regSBUF =&H99 '串行口数据'
const regP2 = &HA0 'P2端口'
const regAUXR1 = &HA2
const regB = &HF0
const regWDTRST = &HA6 '看门狗复位

const regIE = &HA8 '*IE 终端
const regP3 = &HB0 '*P3端口
const regIP = &HB0 '*中断优先权
const regT2CON =&HC8  '定时器2控制
const regT2MOD =&HC9 '定时器2模式
const regRCAP2L = &HCA '捕捉定时器低字节
const regRCAP2H = &HCB '捕捉定时器高字节
const regTL2 = &HCC 'T2低字节
const regH2 = &HCD 'T2高字节
const regPSW = &HD0 '程序状态字
const regA =&HE0 'A累加器
'枚举R0-R7'
enum registerNumber
  rn0 =0
  rn1
  rn2
  rn3
  rn4
  rn5
  rn6
  rn7
end enum
'数据类型定义 dy =datatype'
type dt as ubyte
'微控制器类'
type Mcs51
    private:
    pc as ushort '程序计数器
    insReg_ as ubyte '指令寄存器
    ram(0 to RAM_SIZE -1) as dt '数据存储器
    rom(0 to ROM_SIZE -1) as dt '程序存储器
    extRam(0 to ROM_SIZE -1) as dt '外部数据存储器
    R0_ as dt
    R1_ as dt
    R2_ as dt
    R3_ as dt
    R4_ as dt
    R5_ as dt
    R6_ as dt
    R7_ as dt
    declare sub initRegGroup() '初始化使用哪一个寄存器组
    declare function valhex(byref s as  string) as integer '将字符串转化为16进制的数
    declare sub datarecord(byval buf as string,byval addr as integer) '解析hex文件'
    public:
        mode as integer
        declare constructor()
        declare destructor()
        declare sub calc() '运算器
        declare sub insFetch() '取指
        declare function insReg() as ubyte '获取当前的指令
        declare sub loadhex(byref filename as const string) '加载hex文件并解析'
        declare property R0(val as dt)
        declare property R0() as dt
        declare property R1(val as dt)
        declare property R1() as dt
        declare property R2(val as dt)
        declare property R2() as dt
        declare property R3(val as dt)
        declare property R3() as dt
        declare property R4(val as dt)
        declare property R4() as dt
        declare property R5(val as dt)
        declare property R5() as dt
        declare property R6(val as dt)
        declare property R6() as dt
        declare property R7(val as dt)
        declare property R7() as dt
        declare property A(val as dt)
        declare property A() as dt
        declare property B(val as dt)
        declare property B() as dt
        declare property PSW(val as dt)
        declare property PSW() as dt
        declare property P(val as dt)
        declare property P() as dt
        declare property DPTR(val as ushort)
        declare property DPTR() as ushort
        declare property SP(val as dt)
        declare property SP() as dt
        declare property OV(val as dt)
        declare property OV() as dt
        declare property RS0(val as dt)
        declare property RS0() as dt
        declare property RS1(val as dt)
        declare property RS1() as dt
        declare property F0(val as dt)
        declare property F0() as dt
        declare property Ac(val as dt)
        declare property Ac() as dt
        declare property Cy(val as dt)
        declare property Cy() as dt
        declare property EA(val1 as dt)
        declare property EA() as dt
        declare function getPc() as ushort
        declare function ramGet(pc as ushort) as dt
        declare sub ramSet(pc as ushort,val as dt)
        declare function romGet(pc as ushort) as dt
        declare sub romSet(pc as ushort,val as dt)
        declare function extRamGet(pc as ushort) as dt
        declare sub extRamSet(pc as ushort ,val as dt)
        declare sub bit_set(val1 as dt)
        declare sub bit_set(addr as dt,val1 as dt)
        declare sub isP() '判断并设置A 奇偶'
        declare sub isOV(val1 as dt,val2 as dt) '是否溢出
        declare sub isOV()
        declare sub isAc(val1 as dt,val2 as dt) '低半字节是否进位
        declare sub isCy(val1 as dt,val2 as dt) '最高有效位是否进位
        declare sub add_a_rn(rn as registerNumber)
        declare sub add_a_direct_()
        declare sub add_a_atri(ri as registerNumber)
        declare sub adda_data()
        declare sub addc_a_rn(rn as registerNumber)
        declare sub addc_a_direct_()
        declare sub addc_atri(rn as registerNumber)
        declare sub addc_a_data_()
        declare sub subb_a_rn(rn as registerNumber)
        declare sub subb_a_direct_()
        declare sub subb_a_atri(ri as registerNumber)
        declare sub subb_a_data_()
        declare sub inc_direct_ ()
        declare sub dec_direct_()
        declare sub mul_ab_()
        declare sub div_ab_()
        declare sub da_a_()
        declare sub anl_a_direct_()
        declare sub anl_direct_a_()
        declare sub anl_direct_data_()
        declare sub orl_a_direct_()
        declare sub orl_a_data_()
        declare sub orl_direct_a_()
        declare sub orl_direct_data_()
        declare sub xrl_a_direct_()
        declare sub xrl_direct_data_()
        declare sub rl_a_()
        declare sub rlc_a_()
        declare sub rr_a_()
        declare sub rrc_a_()
        declare sub swap_a_()
        declare sub push()
        declare sub pop()
        declare sub xchd_a_atri(ri as registerNumber)
        declare sub cpl_bit_()
        declare function bit_state(addr as ubyte ) as byte
        declare sub mov_bit_c_()
        declare sub acall_addr11(n as ubyte)
        declare sub lcall()
        declare sub ajmp(n as ubyte)
        declare sub ljmp()
        declare sub sjmp()
        declare sub jz_rel_()
        declare sub njz_rel_()
        declare sub jc_rel_()
        declare sub njc_rel_()
        declare sub jb_bit_rel_()
        declare sub jnb_bit_rel_()
        declare sub jbc_bit_rel_()
        declare sub cjne_a_direct_rel_()
        declare sub cjne_a_data_rel_()
        declare sub cjne_rn_data_rel(rn as registerNumber)
        declare sub cjne_ri_data_rel(ri as registerNumber)
        declare sub djnz_rn_rel(rn as registerNumber)
        declare sub djnz_direct_rel_()
        declare sub print_info()
        declare sub debug()
end type
